package com.daylive.util;

import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.Properties;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


/**
 * 融云chat room api
 * @author mac
 */
public class ChatRoomUtil{
	private final static Logger logger = Logger.getLogger(ChatRoomUtil.class);
	private static String appKey = "qd46yzrf4c6sf";
	private static String appSecret = "w8GIFiZ1fV";
	
	/*public static void main(String[] args) {
		
		SdkHttpResult sdkHttpResult;
		try {
			sdkHttpResult = getRoomkUserCount("aaaaa");
			System.out.println("Response=" + sdkHttpResult.getResult());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	
	// 封禁用户
	public static SdkHttpResult blockUserInChatRoom(String userId,int chatroomId, int minute) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret,
				"http://api.cn.ronghub.com/chatroom/user/gag/add.json" );

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, "UTF-8"));
		sb.append("&chatroomId=").append(URLEncoder.encode(String.valueOf(chatroomId), "UTF-8"));
		sb.append("&minute=").append(URLEncoder.encode(String.valueOf(minute), "UTF-8"));

		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
	
	// 解禁用户
	public static SdkHttpResult relieveUserInChatRoom(String userId,int chatroomId) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret,
				"http://api.cn.ronghub.com/chatroom/user/gag/rollback.json" );

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, "UTF-8"));
		sb.append("&chatroomId=").append(URLEncoder.encode(String.valueOf(chatroomId), "UTF-8"));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
	// 获取被封禁用户
	public static SdkHttpResult queryBlockUsers() throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret,
				"http://api.cn.ronghub.com/user/block/query.json");

		return HttpUtil.returnResult(conn);
	}
	
	// 获取聊天室用户数量
	public static SdkHttpResult getRoomkUserCount(String chatRoomId) throws Exception {

		HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret,
				"https://api.cn.ronghub.com/chatroom/user/query.json");
		StringBuilder sb = new StringBuilder();
		sb.append("chatroomId=").append(URLEncoder.encode(chatRoomId, "UTF-8"));
		HttpUtil.setBodyParameter(sb, conn);
		return HttpUtil.returnResult(conn);
	}
			
	// 获取token
	public static SdkHttpResult getToken(String userId, String userName, String portraitUri) throws Exception {

		HttpURLConnection conn = HttpUtil
				.CreatePostHttpConnection(appKey, appSecret, "http://api.cn.ronghub.com/user/getToken.json" );

		StringBuilder sb = new StringBuilder();
		sb.append("userId=").append(URLEncoder.encode(userId, "UTF-8"));
		sb.append("&name=").append(URLEncoder.encode(userName==null?"":userName, "UTF-8"));
		sb.append("&portraitUri=").append(URLEncoder.encode(portraitUri==null?"":portraitUri, "UTF-8"));
		HttpUtil.setBodyParameter(sb, conn);

		return HttpUtil.returnResult(conn);
	}
}
